package com.example.course_mangger_system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.course_mangger_system.entity.Achievement;
import com.example.course_mangger_system.mapper.AchievementMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 成绩
 * @author: hyj
 * @date: 2023/11/25 15:59
 */
@Service
public class AchievementService {

    @Resource
    private AchievementMapper achievementMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加成绩
     *
     * @param achievement
     * @return
     */
    public int save(Achievement achievement) {
        try {
            int result = achievementMapper.insert(achievement);

            // 更新缓存
            updateCache(achievement.getStuNo());

            return result;
        } catch (Exception e) {
            // 处理异常，可以记录日志或者返回特定的错误码
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 删除成绩
     *
     * @param delete
     * @return
     */
    public int delete(Achievement delete) {
        LambdaQueryWrapper<Achievement> wrapper = Wrappers.lambdaQuery(delete);
        Achievement achievement = achievementMapper.selectOne(wrapper);
        int result = achievementMapper.delete(wrapper);

        // 更新缓存
        updateCache(achievement.getCourseCode() + achievement.getStuNo());

        return result;
    }

    /**
     * 更新成绩
     *
     * @param achievement
     * @return
     */
    public int update(Achievement achievement) {
        int result = achievementMapper.updateById(achievement);
        updateCache(achievement.getCourseCode() + achievement.getStuNo());
        return result;

    }

    /**
     * 查询成绩
     *
     * @param achievement
     * @return
     */

    public List<Achievement> list(Achievement achievement) {
        String cacheKey = "achievement:" + achievement.getCourseCode() + achievement.getStuNo();
        // 尝试从缓存中获取数据
        List<Achievement> cachedAchievements = (List<Achievement>) redisTemplate.opsForValue().get(cacheKey);

        // 如果缓存中有数据，直接返回
        if (cachedAchievements != null) {
            return cachedAchievements;
        }

        // 如果缓存中没有数据，从数据库查询
        LambdaQueryWrapper<Achievement> queryWrapper = Wrappers.lambdaQuery(achievement);
        List<Achievement> achievements = achievementMapper.selectList(queryWrapper);

        // 将查询结果存入缓存，设置适当的过期时间
        redisTemplate.opsForValue().set(cacheKey, achievements);
        return achievements;
    }

    /**
     * 根据ID查询成绩
     *
     * @param id
     * @return
     */
    public Achievement getById(Long id) {
        return achievementMapper.selectById(id);
    }

    private void updateCache(Long userId) {
        String cacheKey = "achievement:" + userId;

        // 清除缓存
        redisTemplate.delete(cacheKey);
    }
}